package com.nc.delivery_project.client.cargo_details;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.core.shared.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.web.bindery.requestfactory.shared.Receiver;
import com.nc.delivery_project.client.ClientFactory;
import com.nc.delivery_project.client.request.CargoRequest;
import com.nc.delivery_project.shared.proxy.CargoProxy;


/**
* Created with IntelliJ IDEA.
* User: Natalie D.
* Date: 1/27/15
* Time: 2:02 PM
*/
public class CargoDetailsActivity extends AbstractActivity implements CargoPresenter {


    private CargoDetailsView view;

    private ClientFactory clientFactory;

    private CargoProxy cargo;

    private int cargoId;

   // private List<CargoProxy> cargoes;

    private CargoRequest updateRequest;


    public CargoDetailsActivity(CargoDetailsPlace place, ClientFactory clientFactory) {
        this.clientFactory = clientFactory;
        parseToken(place.getToken());
        updateRequest = clientFactory.getRequestFactory().createCargoRequest();
    }

    @Override
    public void start(AcceptsOneWidget panel, EventBus eventBus) {

        fetchCargo(cargoId);

        if (view == null) {
            view = new CargoDetailsView();
        }

        view.setPresenter(this);
        panel.setWidget(view);
        view.start();

        view.setPresenter(this);
        panel.setWidget(view);
      //  view.start();

       // view.userIdLbl.setText(String.valueOf(userId));

        initButtonHandlers();
    }

    @Override
    public void loadCargoDetails(CargoProxy cargo) {
        view.cargoIdLbl.setText(String.valueOf(cargo.getId()));
        view.titleTxt.setText(cargo.getTitle());
        view.widthTxt.setText(String.valueOf(cargo.getWidth()));
        view.heightTxt.setText(String.valueOf(cargo.getHeight()));
        view.weightTxt.setText(String.valueOf(cargo.getWeight()));
        view.depthTxt.setText(String.valueOf(cargo.getDepth()));
        view.currencyTxt.setText(cargo.getCurrency().getTitle());
        view.costTxt.setText(String.valueOf(cargo.getCost()));
    }

    @Override
    public void update(CargoProxy cargo) {
        updateRequest.update(cargo).fire();
        Window.Location.reload();
    }

    @Override
    public void delete(int id) {
        clientFactory.getRequestFactory().createCargoRequest().delete(cargoId).fire();
    }


    @Override
    public void goTo(Place place) {
        clientFactory.getPlaceController().goTo(place);
    }

    @Override
    public ClientFactory getClientFactory() {
        return clientFactory;
    }

    private void initButtonHandlers() {
        view.saveCBtn.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                cargo.setTitle(view.titleTxt.getText());
                cargo.setWidth(Integer.parseInt(view.widthTxt.getText()));
                cargo.setDepth(Integer.parseInt(view.depthTxt.getText()));
                cargo.setHeight(Integer.parseInt(view.heightTxt.getText()));
                cargo.setWeight(Integer.parseInt(view.weightTxt.getText()));
                cargo.setCost(Integer.parseInt(view.costTxt.getText()));
              //  cargo.setCurrency(view.currencyTxt.getText());
//                GWT.log(cargo.getTitle()+"\n"+ cargo.getWidth()+"\n"+
//                cargo.getHeight()+"\n"+cargo.getDepth()+"\n"+
//                cargo.getWeight()+"\n"+cargo.getCost()+"\n"+cargo.getCurrency());


              //  update(cargo);
            }
        });

        view.removeCBtn.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                delete(cargo.getId());
            }
        });


    }


    private void fetchCargo(int id) {
        CargoRequest cargoRequest = clientFactory.getRequestFactory().createCargoRequest();
        cargoRequest.read(id).fire(new Receiver<CargoProxy>() {
            @Override
            public void onSuccess(CargoProxy response) {
                GWT.log("cargo fetched");
                cargo = updateRequest.edit(response);
                loadCargoDetails(cargo);
            }

        });
    }

    private void parseToken(String token) {
        try {
            cargoId = Integer.parseInt(token);
        } catch (NumberFormatException e) {
            Window.alert("Unknown Token");
        }
    }
}